package com.example.composestudy.part6

import androidx.compose.foundation.Canvas
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.drawscope.Stroke
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.example.composestudy.utils.initialPage
import com.google.accompanist.pager.ExperimentalPagerApi
import com.google.accompanist.pager.HorizontalPager
import com.google.accompanist.pager.rememberPagerState

@Preview(showBackground = true)
@OptIn(ExperimentalPagerApi::class)
@Composable
fun DrawArc() {
    methods.forEach { println(it.javaClass.name) }
    val initialPage = methods.lastIndex.initialPage()
    val state = rememberPagerState(initialPage)
    HorizontalPager(
        count = methods.size,
        modifier = Modifier.fillMaxWidth(),
        state = state
    ) { page ->
        methods[page].Invoke()
    }
}

interface DrawArc {
    @Composable
    fun Invoke()
}

private val methods = listOf(
    object : DrawArc {
        @Composable
        override fun Invoke() {
            PageItem(text = "DrawArc1") {
                Canvas(modifier = Modifier.size(360.dp)) {
                    drawArc(
                        color = Color.Red,
                        startAngle = 0f, // 起始角度（以度为单位）。0 代表 3 点钟位置
                        sweepAngle = 90f, // 相对于顺时针绘制的弧的大小（以度为单位）
                        useCenter = true, // 指示弧是否要闭合边界中心的标志
                    )
                }
            }
        }
    },
    object : DrawArc {
        @Composable
        override fun Invoke() {
            val width = LocalConfiguration.current.screenWidthDp.dp
            PageItem(text = "DrawArc2") {
                Canvas(
                    modifier = Modifier
                        .size(width)
                        .padding(16.dp)
                ) {
                    drawArc(
                        color = Color.Red,
                        startAngle = 90f,
                        sweepAngle = 120f,
                        useCenter = false,
                    )
                }
            }
        }
    },
    object : DrawArc {
        @Composable
        override fun Invoke() {
            val width = LocalConfiguration.current.screenWidthDp.dp
            PageItem(text = "DrawArc3") {
                Canvas(
                    modifier = Modifier
                        .size(width)
                        .padding(16.dp)
                ) {
                    drawArc(
                        color = Color.Red,
                        startAngle = 90f,
                        sweepAngle = 120f,
                        useCenter = true,
                        style = Stroke(width = 10f)
                    )
                }
            }
        }
    }
)
